<!--
 * @Author: your name
 * @Date: 2021-09-06 19:17:19
 * @LastEditTime: 2021-09-18 22:37:19
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ej-webui\src\pages\lunbo\lunbo.vue
-->
<template>
  <div class="carousel">
    <div class="btns">
      <!-- 检索框 -->
      <el-form :inline="true" :model="params" size="small">
        <el-button @click="addHandler" type="primary" size="small" style="margin-right: 10px" >新增</el-button>
        <el-button @click="viewHandler"  type="primary" size="small" style="margin-right: 10px" >预览</el-button>        

      </el-form>
    </div>
    <div class="content">
      <!-- 表格 -->
      <el-table :data="resData" style="width: 100%" size="small">
        <el-table-column label="序号" prop="id"></el-table-column>
        <el-table-column label="轮播图名称" prop="name"></el-table-column>
        <el-table-column label="轮播图描述" prop="introduce"></el-table-column>
        <el-table-column label="轮播图图片" prop="url">
          <template v-slot="scope">
            <el-popover placement="right" width="225" trigger="hover">
              <img :src="scope.row.url" width="200" alt="图片丢失" />
              <img
                slot="reference"
                :src="scope.row.url"
                width="50"
                alt="图片丢失"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status">
          <template v-slot="scope">
            <el-tag
              v-if="scope.row.status == '正常'"
              size="mini"
              type="success"
              >{{ scope.row.status }}</el-tag>
            <el-tag v-else size="mini" type="danger">{{scope.row.status}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="editHandler(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="delHandler(scope.row.id)" style="color: red">删除</el-button>
            <el-button v-if="scope.row.status == '正常'" type="text" size="small" @click="stopHandler(scope.row.id,$event)" style="color: purple">停用</el-button>
            <el-button v-else type="text" size="small" @click="stopHandler(scope.row.id,$event)" style="color: green">启用</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <!-- total表示：数据的总条数  current-page表示：当前所在的页数   -->
      <el-pagination
        background
        layout="prev, pager, next, sizes"
        :total="resData.total"
        :page-size="params.pageSize"
        :current-page="params.page"
        @current-change="pageChangeHandler"
        :page-sizes="[5, 10, 15]"
        @size-change="pageSizeChangeHandler"
      >
      </el-pagination>

      <!-- 模态框 -->
      <el-dialog :title="title" :visible.sync="visible" @close="dialogCloseHandler">
        <el-form  :model="carousel" :rules="rules" ref="saveForm">
          <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="carousel.name"></el-input>
          </el-form-item>
          <el-form-item  label="图片" :label-width="formLabelWidth" prop="url">
            <el-upload
              class="avatar-uploader"
              action="http://81.69.24.232/pet/jz_upload.php"
              :show-file-list="false"
              :on-success="handlePhotoSuccess">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="描述" :label-width="formLabelWidth" prop="introduce">
            <el-input v-model="carousel.introduce"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSaveHandler">确 定</el-button>
        </div>
      </el-dialog>



      <!-- 预览弹框 -->
      


    </div>
  </div>
</template>
<script>
import request from "@/utils/request";
import { mapActions } from "vuex";
import qs from "qs";
import _ from "lodash";


export default {
  data() {
    return {
       params: {
        page: 1,
        pageSize: 5,
      },
      resData: [],
      title: "",
      carousel: {},
      visible: false,
      formLabelWidth: '80px',
      imageUrl: null,
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur"},
        ],
        url: [
          { required: true, message: "请上传轮播图", trigger: "blur"}
        ],
        introduce: [
          {
            required: true,
            message: "轮播图描述不能为空",
            trigger: "blur"
          }
        ],
      },

    };
  },
  methods: {
    // 导入状态机中的方法
    ...mapActions("category", ["findAllCategories"]),

    // 分页查询产品表格
    async pageQueryProductions() {
        // 过滤数据将空字符串删除
        for (let key in this.params) {
            if (this.params[key] == "") {
                delete this.params[key];
            }
        }
        let res = await request.get("/carousel/findAll", {
            params: this.params,
        });
        this.resData = res.data;
    },
    // page改变事件  
    pageChangeHandler(page) {
        // 将更新了的页码传递给页面   并且刷新查询到的对应页码上的内容
        this.params.page = page;
        this.pageQueryProductions();
    },
    // pageSize改变事件  pageSize是每页中显示的数据条目
    pageSizeChangeHandler(pageSize) {
        this.params.pageSize = pageSize;
        this.pageQueryProductions();
    },

    // 新增按钮  处理函数
    addHandler(){
        this.carousel = {};
        this.imageUrl = null;
        this.title = '新增轮播图';
        this.visible = true;
    },
    // 添加完成后的保存事件
    toSaveHandler(){
        this.$refs["saveForm"].validate((valid) => {
            if(valid) {
                request.post("/carousel/saveOrUpdate", qs.stringify(this.carousel))
                    .then((res) => {
                    this.pageQueryProductions(); // 重载数据
                    this.$message.success(res.message); // 提示操作结果信息
                    this.visible = false; // 关闭模态框
                });
            } else {
                return fasle;
            }
        });
    },
    // 关闭模态框的回调函数
    dialogCloseHandler() {
        // 置空表单验证
        this.$refs["saveForm"].resetFileds();
    },
    // 图片上传成功回调
    handlePhotoSuccess(res) {
        // 将返回的图片在线地址映射到数据模型中
        this.carousel.url = res;
        this.imageUrl = res;
    },

    // 编辑按钮  事件函数
    editHandler(row) {
      // 将当前行数据赋值给数据模型
      this.carousel = _.clone(row);
      this.imageUrl = row.url;
      this.title = "编辑轮播图";
      this.visible = true;
    },
    // 删除 事件
    delHandler(id) {
      this.$confirm('此操作将永久删除该轮播图, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
          request.get('/carousel/deleteById',{ params:{id} }).then((res) => {
              // 刷新数据
              this.pageQueryProductions();
              this.$message({
                  type: "success",
                  message: res.message,
              }); // 提示成功信息
          })
      })
    },
    // 停用按钮事件
    stopHandler(id,e){
            this.$confirm("是否确认该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (e.target.innerText == "停用") {
          requset.get("/carousel/query", { params: { id } }).then((res) => {
            this.pageQueryProductions(); // 重载数据
            this.$message.success(res.message); // 提示信息
          });
        } else {
          requset.get("/carousel/query", { params: { id } }).then((res) => {
            this.pageQueryProductions(); // 重载数据
            this.$message.success(res.message); // 提示信息
          });
        }
      });
    },

  },
  created() {
      this.pageQueryProductions(); // 分页查询产品
      this.findAllCategories();
  },
};
</script>

<style scoped>
/* 图片上传  样式 */
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>